package com.bootdo.jiashan.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.BeanCopierUtil;
import com.bootdo.jiashan.domain.ProductDO;
import com.bootdo.jiashan.model.PdtNormModel;
import com.bootdo.jiashan.service.ProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.jiashan.domain.PdtNormDO;
import com.bootdo.jiashan.service.PdtNormService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 产品(SPU)和品规属性关联表
 * 
 * @author hejinen
 * @email 1992lcg@163.com
 * @date 2019-03-29 22:27:21
 */
 
@Controller
@RequestMapping("/jiashan/pdtNorm")
public class PdtNormController {

	@Autowired
	private PdtNormService pdtNormService;
	@Autowired
	private ProductService productService;
	
	@GetMapping()
	String PdtNorm(){
	    return "jiashan/pdtNorm/pdtNorm";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<PdtNormDO> pdtNormList = pdtNormService.list(query);
		int total = pdtNormService.count(query);
		PageUtils pageUtils = new PageUtils(pdtNormList, total);
		return pageUtils;
	}
	
	@GetMapping("/add/{pdtId}")
	@RequiresPermissions("jiashan:product:add")
	String add(@PathVariable("pdtId")Integer pdtId,String pdtName,Model model){
		model.addAttribute("pdtId", pdtId);
		model.addAttribute("pdtName", pdtName);
	    return "jiashan/pdtNorm/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jiashan:product:edit")
	String edit(@PathVariable("id") Integer id,Model model){

		PdtNormDO pdtNorm = pdtNormService.get(id);
		ProductDO pdtDO = productService.get(pdtNorm.getPdtId());
		PdtNormModel pdtNormModel = new PdtNormModel();
		BeanCopierUtil.copyProperties(pdtNorm,pdtNormModel);
		pdtNormModel.setPdtName(pdtDO.getName());
		model.addAttribute("pdtNorm", pdtNormModel);
	    return "jiashan/pdtNorm/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jiashan:product:add")
	public R save( PdtNormDO pdtNorm){
		if(pdtNormService.save(pdtNorm)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jiashan:product:edit")
	public R update( PdtNormDO pdtNorm){
		pdtNormService.update(pdtNorm);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jiashan:pdtNorm:remove")
	public R remove( Integer id){
		if(pdtNormService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jiashan:pdtNorm:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		pdtNormService.batchRemove(ids);
		return R.ok();
	}
	
}
